package com.leesche.zcsp.node.web.controller.web.v2.device;

import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.utils.NetConfig;
import com.leesche.zcsp.node.utils.PropKit;
import com.leesche.zcsp.node.utils.UUIDGenerateUtil;
import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.utils.web.WebUtils;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.controller.web.v2.admin.WebAdminController;
import com.leesche.zcsp.node.web.model.PubDevParam;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.model.PubDeviceDiscount;
import com.leesche.zcsp.node.web.service.*;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.vo.v2.PlaceDeviceListVo;
import com.leesche.zcsp.node.web.vo.v2.enums.DeviceEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

/**
 * Created by kaider on 2016/12/2.
 */
@Controller
@RequestMapping("/v2/device")
public class WebDeviceController extends WebAdminController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WebDeviceController.class);

    private static final String SHOW_VIEW = "web/v2/device/showView";

    @Autowired
    private PubPlaceService placeService;
    @Autowired
    private PubDeviceService deviceService;
    @Autowired
    private PubDiscountService discountService;
    @Autowired
    private PubDeviceDiscountService deviceDiscountService;
    @Autowired
    private PubDevParamService paramService;
    @Autowired
    private NetService netService;
    @Autowired
    private PubDevGiftStockService stockService;

    /**
     * 设备列表
     * @param placeCondition
     * @param deviceCondition
     * @param model
     * @return
     */
    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition,
            Model model
    ) {
        Long adminId = getAdminId();
        placeCondition.setAdminId(adminId);
        deviceCondition.setAdminId(adminId);
        //场地
        getPlaceList(placeCondition, model);
        //设置设备在线状态
        deviceCondition.setIsOnline(DeviceEnum.ONLINE.getCode());
        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
        PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
        model.addAttribute("cur", DeviceEnum.ONLINE);
        model.addAttribute("deviceList", vo);
        return SHOW_VIEW;
    }

    /**
     * 依据不同的状态查询设备
     * @param placeId
     * @param deviceStats
     * @param placeCondition
     * @param deviceCondition
     * @param model
     * @return
     */
    @RequestMapping(value = "{placeId}/showView/{stats}", method = RequestMethod.GET)
    public String showView(
            @PathVariable("placeId") Long placeId,
            @PathVariable("stats") String deviceStats,
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition,
            Model model
    ) {
        DeviceEnum deviceEnum = DeviceEnum.parse(deviceStats);
        Long adminId = getAdminId();
        placeCondition.setAdminId(adminId);
        placeCondition.setPlaceId(placeId);
        deviceCondition.setAdminId(adminId);
        deviceCondition.setIsOnline(deviceEnum.getCode());
        getPlaceList(placeCondition, model);

        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
        PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
        model.addAttribute("deviceList", vo);
        model.addAttribute("cur", deviceEnum);
        model.addAttribute("placeId", placeId);
        return SHOW_VIEW;
    }

    /**
     * 设备状态页面
     * @param placeId
     * @param deviceStats
     * @param deviceId
     * @param placeCondition
     * @param deviceCondition
     * @param model
     * @return
     */
    @RequestMapping(value = "{placeId}/showView/{stats}/{deviceId}/deviceView", method = RequestMethod.GET)
    public String deviceView(
            @PathVariable("placeId") Long placeId,
            @PathVariable("stats") String deviceStats,
            @PathVariable("deviceId") Long deviceId,
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition,
            QueryPubDiscountCondition discountCondition,
            QueryPubDeviceDiscountCondition deviceDiscountCondition,
            QueryPubDevParamCondition paramCondition,
            Model model
    ) {
        DeviceEnum deviceEnum = DeviceEnum.parse(deviceStats);
        Long adminId = getAdminId();
        String businessNo = getBusinessNo();
        placeCondition.setAdminId(adminId);
        placeCondition.setPlaceId(placeId);
        deviceCondition.setAdminId(adminId);
        discountCondition.setAdminId(adminId);
        deviceCondition.setIsOnline(deviceEnum.getCode());
        paramCondition.setAdminId(adminId);
        paramCondition.setDeviceId(deviceId);

        getPlaceList(placeCondition, model);

        InfoDeviceDto dto = deviceService.findInfoDevice(deviceCondition);
        dto.setBusinessNo(businessNo);

        //多局二维码
        String payUrlTemp = PropKit.use("rqcode.properties").get("payCode1");
        String payUrl = payUrlTemp.replace("PAYCODE", dto.getMac());
        //设备套餐
        List<PubDiscountDto> discountList = getDiscountList(discountCondition, deviceDiscountCondition);
        //设备参数
        PubDevParamDto paramDto = paramService.findByCondition(paramCondition);

        model.addAttribute("cur", deviceEnum);
        model.addAttribute("placeId", placeId);
        model.addAttribute("deviceId", deviceId);
        model.addAttribute("infoDevice", dto);
        model.addAttribute("payUrl", payUrl);
        model.addAttribute("discountList", discountList);
        model.addAttribute("paramDto", paramDto);
        model.addAttribute("view", 2);
        return SHOW_VIEW;
    }

    /**
     * 查询页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchView", method = RequestMethod.GET)
    public String searchView(
            QueryPubPlaceCondition placeCondition,
            Model model
    ) {
        Long adminId = getAdminId();
        placeCondition.setAdminId(adminId);
        getPlaceList(placeCondition, model);
        model.addAttribute("view", 3);
        return SHOW_VIEW;
    }

    /**
     * 场地查询设备
     * @param deviceCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchPlaceDevice", method = RequestMethod.POST)
    public Map<String, Object> searchPlaceDevice(
            QueryPubDeviceCondition deviceCondition
    ) {
        try {
            Long adminId = getAdminId();
            deviceCondition.setAdminId(adminId);
            List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
            return WebResult.success().put("data", deviceList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 条件查询设备
     * @param placeCondition
     * @param deviceCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchDevice", method = RequestMethod.POST)
    public Map<String, Object> searchDevice(
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition
    ) {
        Long adminId = getAdminId();
        deviceCondition.setAdminId(adminId);
        placeCondition.setAdminId(adminId);
        List<PlaceDeviceListVo> placeDevicesList = new ArrayList<PlaceDeviceListVo>();
        try {
            if (StringUtils.isNotBlank(deviceCondition.getImme()) ||
                    StringUtils.isNotBlank(deviceCondition.getDeviceshortcode()) ||
                    deviceCondition.getDeviceId() != 0) {

                List<InfoDeviceDto> deviceTempList = deviceService.findDeviceList(deviceCondition);
                List<Long> placeIdList = new ArrayList<Long>();
                for (InfoDeviceDto dto : deviceTempList) {
                    placeCondition.setPlaceId(dto.getPlaceId());
                    PubPlaceDto placeDto = placeService.findByCondition(placeCondition);
                    placeIdList.add(placeDto.getPlaceid());
                }
                //场地id去重
                placeIdList = new ArrayList(new HashSet(placeIdList));
                //设置场地
                for (int i = 0; i < placeIdList.size(); i++) {
                    Long placeId = placeIdList.get(i);
                    placeCondition.setPlaceId(placeId);
                    PubPlaceDto placeDto = placeService.findByCondition(placeCondition);
                    List<InfoDeviceDto> deviceList = new ArrayList<InfoDeviceDto>();
                    for (InfoDeviceDto dto : deviceTempList) {
                        if (dto.getPlaceId().equals(placeId)) {
                            deviceList.add(dto);
                        }
                    }
                    PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                    vo.setPlaceDto(placeDto);
                    placeDevicesList.add(vo);
                }
            } else {
                if (deviceCondition.getPlaceId() == 0 && deviceCondition.getDeviceId() == 0) {
                    List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);
                    for (PubPlaceDto dto : placeList) {
                        deviceCondition.setPlaceId(dto.getPlaceid());
                        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
                        PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                        vo.setPlaceDto(dto);
                        placeDevicesList.add(vo);
                    }
                } else {
                    PubPlaceDto dto = placeService.findByCondition(placeCondition);
                    deviceCondition.setPlaceId(dto.getPlaceid());
                    List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
                    PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                    vo.setPlaceDto(dto);
                    placeDevicesList.add(vo);
                }
            }
            return WebResult.success().put("data", placeDevicesList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备远程补币
     * @param deviceId
     * @param coin
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceCoin", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceCoin(
            @PathVariable("deviceId") Long deviceId,
            Integer coin
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                int deviceTypeId = Integer.parseInt(pubDevice.getDevicetypeid().toString());
                boolean flagLine = netService.isOnline(pubDevice.getImme());
                if (!flagLine) {
                    return WebResult.failure("设备已离线").toMap();
                } else if (pubDevice.getDp() < 15) {
                    return WebResult.failure("设备信号弱,请稍后再试").toMap();
                }

                boolean flagCommand = netService.sendClientBuy(pubDevice, 0, coin, NetConfig.PAYTYPE_GAMECOINBU, null);
                if (flagCommand == true) {
                    return WebResult.success().info("指令发送成功").toMap();
                } else {
                    return WebResult.failure("指令发送失败").toMap();
                }
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备图片
     * @param deviceId
     * @param imageUploadFile
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceImg", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceImg(
            @PathVariable("deviceId") Long deviceId,
            @RequestParam(value = "imageFile", required = false) MultipartFile imageUploadFile
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                if (imageUploadFile.isEmpty()) {
                    return WebResult.failure("请选择图片").toMap();
                }

                String fileName = imageUploadFile.getOriginalFilename();
                String extName = fileName.substring(fileName.lastIndexOf("."));

                if (!WebUtils.checkIsImage(extName)
                        && !imageUploadFile.getContentType().matches("image.*")) {
                    return WebResult.failure("上传的文件必须是图片!").toMap();
                }

                String hexString = WebUtils.getFileHexString(imageUploadFile.getInputStream());

                boolean flag = false;
                String[] magic = Constants.SYSTEM_IMAGE_MAGICNUMBER;
                for (int i = 0; i < magic.length; i++) {
                    if (magic[i].equals(hexString)) {
                        flag = true;
                        break;
                    }
                }

                if (flag == false) {
                    return WebResult.failure("上传的文件不是图片类型").toMap();
                }

                if (imageUploadFile.getSize() > Constants.SYSTEM_IMAGE_SIZE) {
                    return WebResult.failure("上传的图片实在是太大了,不能超过" + Constants.SYSTEM_IMAGE_SIZE + "M").toMap();
                }

                String imagePath = SessionManager.getContext().getRealPath("").replace(Constants.SITE_NAME, Constants.SYSTEM_TMAGE_DEVICE_DIRECTORY);
                String imageName = UUIDGenerateUtil.generateUUID() + extName;

                File imageFile = new File(imagePath, imageName);

                if (!imageFile.exists()) {
                    imageFile.mkdirs();
                }

                imageUploadFile.transferTo(imageFile);

                pubDevice.setDeviceImageUrl(SessionManager.getWebServerURL() + Constants.SYSTEM_TMAGE_DEVICE_DIRECTORY + "/" + imageName);
                deviceService.updateByPrimaryKeySelective(pubDevice);
                return WebResult.success().info("上传成功").put("data", pubDevice.getDeviceImageUrl()).toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备套餐
     * @param deviceId
     * @param condition
     * @param discounts
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceDiscount", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceDiscount(
            @PathVariable("deviceId") Long deviceId,
            QueryPubDeviceDiscountCondition condition,
            Long[] discounts
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                Long adminId = pubDevice.getAdminid();
                condition.setAdminId(adminId);
                condition.setDeviceId(deviceId);
                deviceDiscountService.deleteDeviceDiscount(condition);

                if(discounts!=null){
                    for (Long discountId : discounts) {
                        PubDeviceDiscount deviceDiscount = new PubDeviceDiscount();
                        deviceDiscount.setDiscountid(discountId);
                        deviceDiscount.setDeviceid(condition.getDeviceId());
                        deviceDiscount.setAdminid(adminId);
                        deviceDiscountService.insert(deviceDiscount);
                    }
                }
                 return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备参数
     * @param deviceId
     * @param paramCondition
     * @param paramNum       第几个参数
     * @param paramVal       参数值
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceParam", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceParam(
            @PathVariable("deviceId") Long deviceId,
            QueryPubDevParamCondition paramCondition,
            Integer paramNum,
            Integer paramVal
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                paramCondition.setAdminId(pubDevice.getAdminid());
                paramCondition.setDeviceId(deviceId);
                PubDevParam devParam = getPubDevParam(paramNum, paramVal, paramCondition);
                if (devParam.getParamid() != null) {
                    paramService.updateByPrimaryKeySelective(devParam);
                } else {
                    paramService.insert(devParam);
                }
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备名称
     * @param deviceId
     * @param deviceName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceName", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceName(
            @PathVariable("deviceId") Long deviceId,
            String deviceName
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                pubDevice.setDevicename(deviceName);
                deviceService.updateByPrimaryKeySelective(pubDevice);
                return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备启用状态
     * @param deviceId
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceState", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceState(
            @PathVariable("deviceId") Long deviceId,
            Integer state
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                if (pubDevice.getIsonline() != 1) {
                    return WebResult.failure("设备离线").toMap();
                }
                int deviceTypeId = Integer.parseInt(pubDevice.getDevicetypeid().toString());
                if (state == 1) {
                    netService.sendClientStartOrClose(pubDevice.getImme(), deviceTypeId, true);
                    pubDevice.setState(state);
                } else {
                    netService.sendClientStartOrClose(pubDevice.getImme(), deviceTypeId, false);
                    pubDevice.setState(state);
                }
                deviceService.updateByPrimaryKeySelective(pubDevice);
                return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备优惠码状态
     * @param deviceId
     * @param state
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceCouponState", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceCouponState(
            @PathVariable("deviceId") Long deviceId,
            Integer state
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                pubDevice.setIsdiscode(state);
                deviceService.updateByPrimaryKeySelective(pubDevice);
                return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备场地
     * @param deviceId
     * @param place
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDevicePlace", method = RequestMethod.POST)
    public Map<String, Object> updateDevicePlace(
            @PathVariable("deviceId") Long deviceId,
            Long place
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                pubDevice.setPlaceid(place);
                deviceService.updateByPrimaryKeySelective(pubDevice);
                return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 修改设备绑定状态
     * @param deviceId
     * @param deviceDiscountCondition
     * @param statisticsCondition
     * @param stockCondition
     * @param paramCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{deviceId}/updateDeviceBindState", method = RequestMethod.POST)
    public Map<String, Object> updateDeviceBindState(
            @PathVariable("deviceId") Long deviceId,
            QueryPubDeviceDiscountCondition deviceDiscountCondition,
            QueryPubDevicePayDetailStatisticsCondition statisticsCondition,
            QueryPubDevGiftStockCondition stockCondition,
            QueryPubDevParamCondition paramCondition
    ) {
        try {
            PubDevice pubDevice = getPubDevice(deviceId);
            if (pubDevice != null) {
                Long adminId = pubDevice.getAdminid();
                deviceDiscountCondition.setAdminId(adminId);
                statisticsCondition.setUserId(adminId);
                stockCondition.setAdminId(adminId);
                //解除设备绑定的套餐
                deviceDiscountService.deleteDeviceDiscount(deviceDiscountCondition);
                //解除设备绑定的礼品
                stockService.deleteDevGiftStock(stockCondition);
                //解除设备绑定的参数
                paramService.deleteDevParam(paramCondition);
                //解除设备关联
                pubDevice.setAdminid(0L);
                pubDevice.setDevicetypeid(0L);
                pubDevice.setPlaceid(0L);
                pubDevice.setDeviceImageUrl("");
                pubDevice.setDevicename("");
                pubDevice.setIsdiscode(0);
                deviceService.updateByPrimaryKeySelective(pubDevice);

                return WebResult.success().toMap();
            }
            return WebResult.failure("没有权限").toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 场地列表
     * @param placeCondition
     * @param model
     * @return
     */
    private Model getPlaceList(
            QueryPubPlaceCondition placeCondition,
            Model model
    ) {
        List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);
        model.addAttribute("placeList", placeList);
        return model;
    }

    /**
     * 设备分类
     * @param deviceList
     * @return
     */
    private PlaceDeviceListVo getPlaceDeviceListVo(
            List<InfoDeviceDto> deviceList
    ) {
        PlaceDeviceListVo vo = new PlaceDeviceListVo();
        //礼品机
        List<InfoDeviceDto> giftList = new ArrayList<InfoDeviceDto>();
        //兑币机
        List<InfoDeviceDto> currencyList = new ArrayList<InfoDeviceDto>();
        //娃娃机
        List<InfoDeviceDto> dollList = new ArrayList<InfoDeviceDto>();
        //礼品兑换机
        List<InfoDeviceDto> giftExchangeList = new ArrayList<InfoDeviceDto>();

        for (InfoDeviceDto deviceDto : deviceList) {
            String stateName = DeviceEnum.parse(deviceDto.getState()).getMessage();
            deviceDto.setStateName(stateName);
            switch (deviceDto.getDeviceTypeId().intValue()) {
                //礼品
                case 1:
                    giftList.add(deviceDto);
                    break;
                //兑币
                case 2:
                    currencyList.add(deviceDto);
                    break;
                //娃娃
                case 3:
                    dollList.add(deviceDto);
                    break;
                //礼品兑换
                case 4:
                    giftExchangeList.add(deviceDto);
                    break;
                default:
                    break;
            }
        }
        vo.setCurrencyList(currencyList);
        vo.setDollList(dollList);
        vo.setGiftList(giftList);
        vo.setGiftExchangeList(giftExchangeList);

        return vo;
    }

    /**
     * 获取设备套餐列表
     * @param discountCondition
     * @param deviceDiscountCondition
     * @return
     */
    private List<PubDiscountDto> getDiscountList(
            QueryPubDiscountCondition discountCondition,
            QueryPubDeviceDiscountCondition deviceDiscountCondition
    ) {
        List<PubDiscountDto> discountList = discountService.findDiscountList(discountCondition);
        List<PubDiscountDto> discountListTemp = new ArrayList<PubDiscountDto>();
        for (PubDiscountDto discountDto : discountList) {
            deviceDiscountCondition.setDiscountId(Long.valueOf(discountDto.getDiscountid()));
            Integer flag = deviceDiscountService.selectCheckDeviceDiscount(deviceDiscountCondition);
            if (flag == 1) {
                discountDto.setFlag(1);
            } else {
                discountDto.setFlag(0);
            }
            discountListTemp.add(discountDto);
        }
        return discountListTemp;
    }

    /**
     * 判断用户是否有权限进行操作
     * @param deviceId
     * @return
     */
    private PubDevice getPubDevice(
            Long deviceId
    ) {
        Long adminId = getAdminId();
        PubDevice pubDevice = deviceService.selectByPrimaryKey(deviceId);
        if (pubDevice.getAdminid() != null) {
            if (!adminId.equals(pubDevice.getAdminid()) && pubDevice.getAdminid() != 0) {
                return null;
            } else if (adminId.equals(pubDevice.getAdminid())) {
                return pubDevice;
            }
        }
        return null;
    }

    /**
     * 设备参数设置
     * @param paramNum
     * @param paramVal
     * @param paramCondition
     * @return
     */
    private PubDevParam getPubDevParam(
            Integer paramNum,
            Integer paramVal,
            QueryPubDevParamCondition paramCondition
    ) {
        PubDevParam devParam = null;
        PubDevParamDto paramDto = paramService.findByCondition(paramCondition);
        if (paramDto != null) {
            devParam = paramService.selectByPrimaryKey(paramDto.getParamid());
        } else {
            devParam = new PubDevParam();
        }
        switch (paramNum) {
            case 1:
                devParam.setParam1(paramVal);
                break;
            case 2:
                devParam.setParam2(paramVal);
                break;
            case 3:
                devParam.setParam3(paramVal);
                break;
            case 4:
                devParam.setParam4(paramVal);
                break;
            case 5:
                devParam.setParam5(paramVal);
                break;
            case 6:
                devParam.setParam6(paramVal);
                break;
            default:
                break;
        }
        return devParam;
    }
}
